package com.wedata.海康云;

import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.wedata.HttpUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Base64;

public class 人脸图片处理 extends BaseService{

    public static void main(String[] args) {
        //delFace();
        uploadFace();
        //searchFace();
    }

    /**
     * 人脸搜索
     * @return
     */
    public static String searchFace() {
        JSONObject param = new JSONObject();
        param.set("productCode", productCode);
        param.set("projectId", projectId);
        param.set("personIds", new Long[]{792996529064L});
        //{"msg":"","code":"200","data":{"list":[{"faceUrl":"https://piced.hikyun.com/downloadData/photo/563120140080040.jpg?KEY2=2597d1a4977&KEY1=f91de4124e09e82d448876f2e93f882f&accessKey=17a848f2&v=1","score":79,"personId":"792996529064"}],"total":1}}
        //{"msg":"","code":"200","data":{"list":[{"faceUrl":"https://piced.hikyun.com/downloadData/photo/1221992510376.jpg?KEY2=2597d1affa9&KEY1=2e5174a18c3817831710c62992d5bdad&accessKey=17a848f2&v=1","score":79,"personId":"792996529064"}],"total":1}}
        JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
        JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/fdcs/v1/person/face/search", header, param);
        System.out.println(jsonObject);
        return null;
    }

    /**
     * 上传人脸
     * @return
     */
    public static String uploadFace() {
        try {
            JSONObject param = new JSONObject();
            param.set("productCode", productCode);
            param.set("projectId", projectId);

            File file = new File("E:\\1356126708888.jpg");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
            BufferedImage read = ImageIO.read(file);
            ImageIO.write(read, "jpg", baos);//写入流中 -- 海康貌似都是jpg接受存储的-- jpg是经过压缩的，保证流数据较小107K,如果是png则要1892K
            byte[] bytes = baos.toByteArray();//转换成字节
            String encodedText = Base64.getEncoder().encodeToString(bytes);//转换成base64串

            param.set("faceData", encodedText);
            param.set("personId", "282397848593560");
            System.out.println(encodedText);

            JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
            JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/fdcs/v1/person/face/upload", header, param);
            System.out.println(jsonObject);
            //{"msg":"","code":"200","data":{"level":"中","faceId":"563120140080040","faceUrl":"https://piced.hikyun.com/downloadData/photo/563120140080040.jpg?KEY2=259725eafed&KEY1=d8df6ef9ee772109c645dbe321b89b77&accessKey=17a848f2&v=1","score":79,"faceDeUrl":"https://piced.hikyun.com/downloadData/photo/563120140080040.jpg?KEY2=259725eafed&KEY1=d8df6ef9ee772109c645dbe321b89b77&accessKey=17a848f2&v=1"}}
            //{"msg":"","code":"200","data":{"level":"中","faceId":"1221992510376","faceUrl":"https://piced.hikyun.com/downloadData/photo/1221992510376.jpg?KEY2=2597d1afea3&KEY1=3a20382fca91768f6342729f4fa92122&accessKey=17a848f2&v=1","score":79,"faceDeUrl":"https://piced.hikyun.com/downloadData/photo/1221992510376.jpg?KEY2=2597d1afea3&KEY1=3a20382fca91768f6342729f4fa92122&accessKey=17a848f2&v=1"}}
            //{"msg":"","code":"200","data":{"level":"中","faceId":"1127901437496232","faceUrl":"https://piced.hikyun.com/downloadData/photo/1127901437496232.jpg?KEY2=2597e70fcc1&KEY1=76a79cae2eb9cd8f9765525efbd3b202&accessKey=17a848f2&v=1","score":79,"faceDeUrl":"https://piced.hikyun.com/downloadData/photo/1127901437496232.jpg?KEY2=2597e70fcc1&KEY1=76a79cae2eb9cd8f9765525efbd3b202&accessKey=17a848f2&v=1"}}
            //重复上传，会替换人脸URL地址，原地址还有效

        }catch (Exception exception){

        }
        return null;
    }

    /**
     * 删除人脸
     * @return
     */
    public static String delFace() {
        JSONObject param = new JSONObject();
        param.set("productCode", productCode);
        param.set("projectId", projectId);
        //param.set("faceIds", new String[]{"563120140080040"});
        param.set("personIds", new String[]{"792996529064"});
        //删除人脸，人脸图片还在，只是删除了URL地址，这个地址实际上是还在的
        //{"msg":"","code":"200","data":{"list":[{"faceUrl":"https://piced.hikyun.com/downloadData/photo/563120140080040.jpg?KEY2=2597d1a4977&KEY1=f91de4124e09e82d448876f2e93f882f&accessKey=17a848f2&v=1","score":79,"personId":"792996529064"}],"total":1}}
        //{"msg":"","code":"200","data":{"list":[{"faceUrl":"https://piced.hikyun.com/downloadData/photo/1221992510376.jpg?KEY2=2597d1affa9&KEY1=2e5174a18c3817831710c62992d5bdad&accessKey=17a848f2&v=1","score":79,"personId":"792996529064"}],"total":1}}
        JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
        JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/fdcs/v1/person/face/delete", header, param);
        System.out.println(jsonObject);
        return null;
    }

    /*public static void getFace() {
        *//*String BASE_URL = "http://ai12.soluai.com:8573";
        String APP_KEY = "a92b4c9d0ca05193683713072fad82b3";*//*
        String BASE_URL = "http://118.24.26.244:9096";
        String APP_KEY = "4200efc681e2d01dca19dea30f2bca6b";

        String str = "/personnel/info"+"personnel_id="+123+APP_KEY;
        String checksum = MD5.create().digestHex(str);
        String ss = HttpUtil.get(BASE_URL+"/personnel/info?personnel_id="+123+"&checksum="+checksum);

        System.out.println(ss);
    }*/
}
